Skip to content

Docker 安装 Nginx

docker hub: https://hub.docker.com/_/nginx

创建容器挂载的文件夹

shell
# 日志文件目录 
mkdir -p /opt/nginx/logs
#项目文件目录(这里可以存放web文件)
mkdir -p /opt/nginx/html
# 配置文件目录
mkdir -p /opt/nginx/conf.d

# 可以直接一条命令创建多个文件夹
mkdir -p /opt/nginx/logs /opt/nginx/html /opt/nginx/conf.d

创建容器并挂载目录

挂载操作会直接将两个文件夹内容同步,若是直接用宿主机的空文件夹直接挂载到容器内部的配置文件目录上,会造成 nginx 容器配置文件目录被同步为空文件夹,进而导致容器启动失败。故我们需要先启动一遍容器,将初始配置拷贝出来。

shell
# 创建容器
docker run -d --name nginx --restart=always -p 80:80 nginx:1.27.0

# 确认一下容器内的目录位置是否存在
docker exec -it nginx /bin/bash

cd /etc/nginx && ls -l
cd /usr/share/nginx/html && ls -l
cd /var/log/nginx && ls -l

# 复制容器配置文件到宿主机
docker cp nginx:/etc/nginx/nginx.conf /opt/nginx/nginx.conf
docker cp nginx:/etc/nginx/conf.d /opt/nginx/
docker cp nginx:/usr/share/nginx/html /opt/nginx/

# 授权文件夹,防止 nginx 操作文件夹权限不足
chmod -R a+rwx /opt/nginx

# 删除容器
docker rm -f nginx

# 启动容器并挂载目录
docker run -d --name nginx -p 80:80 -v /opt/nginx/logs:/var/log/nginx -v /opt/nginx/html:/usr/share/nginx/html -v /opt/nginx/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx/conf.d:/etc/nginx/conf.d nginx:1.27.0

修改配置文件 /opt/nginx/nginx.conf

修改配置文件(gzip压缩、client_max_body_size 等参数):/opt/nginx/nginx.conf

shell
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    # 修改大一点,默认 1M 不够用
    client_max_body_size 1024m;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;
    
    # 开启 gzip 压缩
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 9;
    gzip_types text/plain text/css application/xml application/javascript application/x-javascript text/javascript;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\.";

    include /etc/nginx/conf.d/*.conf;
}

就修改以上两处。

然后,进入挂载的配置文件夹 /opt/nginx/conf/conf.d/ 下。

修改 default.conf, 或者新建 conf 文件,在这个目录下,.conf 后缀的文件都会被读取为配置文件(因为 /opt/nginx/conf/nginx.conf 中配置了 include /etc/nginx/conf.d/*.conf)

这里以修改 default.conf 文件为例:

shell
server {
    listen 80;
    server_name *.aday.fun;
    rewrite ^(.*)$ https://$host$1 permanent;
}

server {
    listen       443 ssl;
    server_name  home.aday.fun;
    ssl_certificate /etc/nginx/ssl/fullchain.cer;
    ssl_certificate_key /etc/nginx/ssl/*.aday.fun.key;

    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
        #try_files $uri $uri/ /index.html;
        try_files $uri $uri.html $uri/ =404;

        error_page 404 /404.html;
        error_page 403 /404.html;
    }
}

server {
    listen      443 ssl;
    server_name vitality.aday.fun;
    ssl_certificate /etc/nginx/ssl/fullchain.cer;
    ssl_certificate_key /etc/nginx/ssl/*.aday.fun.key;

    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://localhost:8080/;
    }
}

server {
    listen      443 ssl;
    server_name jenkins.aday.fun;
    ssl_certificate /etc/nginx/ssl/fullchain.cer;
    ssl_certificate_key /etc/nginx/ssl/*.aday.fun.key;

    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://localhost:8090/;
    }
}

以上配置,配置了 4 个 server,实际生产换下,可以每一个 server 配置拆开,单独一个 .conf 文件。

比如 home.conf、vitality.conf、jenkins.conf 文件,全部放在 /opt/nginx/conf/conf.d/ 文件夹下即可。

更多配置内容参考:Nginx 部署 vue 及 SpringBoot 前后端分离工程

上传 web 文件到挂载目录中

比如:将你的前端 npm run build 打包后生成的部署文件夹 dist/ 目录里面的所有文件上传到 /opt/nginx/html/ 这个目录中。

重启 nginx 容器,使配置生效

shell
docker restart nginx

访问网页测试

浏览器访问 http://localhost:80

若报错或者启动失败,可查看日志排除错误

shell
docker logs nginx